package com.simplemobiletools.camera.activities

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.extensions.appConfig
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.RadioItem
import kotlinx.android.synthetic.main.activity_settings.*

class SettingsActivity : AppCompatActivity() {
    @SuppressLint("MissingSuperCall")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)
    }

    override fun onResume() {
        super.onResume()

        setupPurchaseThankYou()
        setupCustomizeColors()
        setupSound()
        setupFocusBeforeCapture()
        setupVolumeButtonsAsShutter()
        setupTurnFlashOffAtStartup()
        setupFlipPhotos()
        setupKeepSettingsVisible()
        setupAlwaysOpenBackCamera()
        setupSavePhotoMetadata()
        setupSavePhotosFolder()
        setupPhotoQuality()
        updateTextColors(settings_holder)
        setupSectionColors()
        invalidateOptionsMenu()
    }

    private fun setupSectionColors() {
        val adjustedPrimaryColor = getAdjustedPrimaryColor()
        arrayListOf(shutter_label, startup_label, saving_label).forEach {
            it.setTextColor(adjustedPrimaryColor)
        }
    }

    private fun setupPurchaseThankYou() {
        settings_purchase_thank_you_holder.beGoneIf(isOrWasThankYouInstalled())
        settings_purchase_thank_you_holder.setOnClickListener {
            launchPurchaseThankYouIntent()
        }
    }

    private fun setupCustomizeColors() {
        settings_customize_colors_label.text = getCustomizeColorsString()
        settings_customize_colors_holder.setOnClickListener {
            // TODO 启动颜色设置
            // handleCustomizeColorsClick()
        }
    }

    private fun getLastPart(path: String): String {
        val humanized = humanizePath(path)
        return humanized.substringAfterLast("/", humanized)
    }

    private fun setupSound() {
        settings_sound.isChecked = appConfig.isSoundEnabled
        settings_sound_holder.setOnClickListener {
            settings_sound.toggle()
            appConfig.isSoundEnabled = settings_sound.isChecked
        }
    }

    private fun setupFocusBeforeCapture() {
        settings_focus_before_capture.isChecked = appConfig.focusBeforeCapture
        settings_focus_before_capture_holder.setOnClickListener {
            settings_focus_before_capture.toggle()
            appConfig.focusBeforeCapture = settings_focus_before_capture.isChecked
        }
    }

    private fun setupVolumeButtonsAsShutter() {
        settings_volume_buttons_as_shutter.isChecked = appConfig.volumeButtonsAsShutter
        settings_volume_buttons_as_shutter_holder.setOnClickListener {
            settings_volume_buttons_as_shutter.toggle()
            appConfig.volumeButtonsAsShutter = settings_volume_buttons_as_shutter.isChecked
        }
    }

    private fun setupTurnFlashOffAtStartup() {
        settings_turn_flash_off_at_startup.isChecked = appConfig.turnFlashOffAtStartup
        settings_turn_flash_off_at_startup_holder.setOnClickListener {
            settings_turn_flash_off_at_startup.toggle()
            appConfig.turnFlashOffAtStartup = settings_turn_flash_off_at_startup.isChecked
        }
    }

    private fun setupFlipPhotos() {
        settings_flip_photos.isChecked = appConfig.flipPhotos
        settings_flip_photos_holder.setOnClickListener {
            settings_flip_photos.toggle()
            appConfig.flipPhotos = settings_flip_photos.isChecked
        }
    }

    private fun setupKeepSettingsVisible() {
        settings_keep_settings_visible.isChecked = appConfig.keepSettingsVisible
        settings_keep_settings_visible_holder.setOnClickListener {
            settings_keep_settings_visible.toggle()
            appConfig.keepSettingsVisible = settings_keep_settings_visible.isChecked
        }
    }

    private fun setupAlwaysOpenBackCamera() {
        settings_always_open_back_camera.isChecked = appConfig.alwaysOpenBackCamera
        settings_always_open_back_camera_holder.setOnClickListener {
            settings_always_open_back_camera.toggle()
            appConfig.alwaysOpenBackCamera = settings_always_open_back_camera.isChecked
        }
    }

    private fun setupSavePhotoMetadata() {
        settings_save_photo_metadata.isChecked = appConfig.savePhotoMetadata
        settings_save_photo_metadata_holder.setOnClickListener {
            settings_save_photo_metadata.toggle()
            appConfig.savePhotoMetadata = settings_save_photo_metadata.isChecked
        }
    }

    private fun setupSavePhotosFolder() {
        settings_save_photos.text = getLastPart(appConfig.savePhotosFolder)
        settings_save_photos_holder.setOnClickListener {
            // TODO 文件路径选择
//            FilePickerDialog(this, appConfig.savePhotosFolder, false, showFAB = true) {
//                val path = it
//                handleSAFDialog(it) {
//                    if (it) {
//                        appConfig.savePhotosFolder = path
//                        settings_save_photos.text = getLastPart(appConfig.savePhotosFolder)
//                    }
//                }
//            }
        }
    }

    private fun setupPhotoQuality() {
        updatePhotoQuality(appConfig.photoQuality)
        settings_photo_quality_holder.setOnClickListener {
            val items = arrayListOf(
                    RadioItem(100, "100%"),
                    RadioItem(95, "95%"),
                    RadioItem(90, "90%"),
                    RadioItem(85, "85%"),
                    RadioItem(80, "80%"),
                    RadioItem(75, "75%"),
                    RadioItem(70, "70%"),
                    RadioItem(65, "65%"),
                    RadioItem(60, "60%"),
                    RadioItem(55, "55%"),
                    RadioItem(50, "50%"))

            RadioGroupDialog(this@SettingsActivity, items, appConfig.photoQuality) {
                appConfig.photoQuality = it as Int
                updatePhotoQuality(it)
            }
        }
    }

    private fun updatePhotoQuality(quality: Int) {
        settings_photo_quality.text = "$quality%"
    }
}
